package com.industrial.operation.service.impl;

import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.operation.domain.OperationCompanyInfo;
import com.industrial.operation.mapper.CompanyHxMapper;
import com.industrial.operation.mapper.OperationCompanyInfoMapper;
import com.industrial.operation.service.IOperationCompanyInfoService;
import com.industrial.operation.vo.OperationGroupAreaVo;
import com.industrial.system.api.model.LoginUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class OperationCompanyInfoServiceImpl implements IOperationCompanyInfoService {

    @Autowired
    private OperationCompanyInfoMapper operationCompanyInfoMapper;


    @Override
    public List<OperationCompanyInfo> getByAreaList(OperationCompanyInfo operationCompanyInfo) {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
            String county=  loginUser.getSysUser().getDistrict();
            operationCompanyInfo.setCounty(county);
        }else if(roles.contains("s_01_011")){
            String city=  loginUser.getSysUser().getCity();
            operationCompanyInfo.setCity(city);
        }else if(roles.contains("p_01_011")){
            String province=  loginUser.getSysUser().getProvince();
            operationCompanyInfo.setProvince(province);
        }else if(loginUser.getUsername().contains("admin")&& com.industrial.common.core.utils.StringUtils.isNull(operationCompanyInfo.getCompanyName())){
             operationCompanyInfo.setProvince("370000");

        }
        return operationCompanyInfoMapper.getByAreaList(operationCompanyInfo);
    }

    @Override
    public List<OperationGroupAreaVo> groupArea(OperationCompanyInfo operationCompanyInfo) {

/*       if(StringUtils.isNotBlank(operationCompanyInfo.getProvince())||StringUtils.isNotBlank(operationCompanyInfo.getCity())
               ||StringUtils.isNotBlank(operationCompanyInfo.getCounty())){

       } else{
           LoginUser loginUser = SecurityUtils.getLoginUser();
           Set<String> roles= loginUser.getRoles();
           if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
               String county=  loginUser.getSysUser().getDistrict();
               operationCompanyInfo.setCounty(county);
               operationCompanyInfo.setCity("");
           }else if(roles.contains("s_01_011")){
               String city=  loginUser.getSysUser().getCity();
               operationCompanyInfo.setCity(city);
           }else if(roles.contains("p_01_011")){
               String province=  loginUser.getSysUser().getProvince();
               operationCompanyInfo.setProvince(province);
           }
       }*/
        if(StringUtils.isNotBlank(operationCompanyInfo.getCity())){
            LoginUser loginUser = SecurityUtils.getLoginUser();
            Set<String> roles= loginUser.getRoles();
            if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
                operationCompanyInfo.setVillage(operationCompanyInfo.getCity());
                operationCompanyInfo.setCity("");
                operationCompanyInfo.setCounty("");
            }else if(roles.contains("s_01_011")){
                operationCompanyInfo.setCounty(operationCompanyInfo.getCity());
                operationCompanyInfo.setCity("");
            }
        }else{
            LoginUser loginUser = SecurityUtils.getLoginUser();
            Set<String> roles= loginUser.getRoles();
            if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
                String county=  loginUser.getSysUser().getDistrict();
                operationCompanyInfo.setCounty(county);
                operationCompanyInfo.setCity("");
            }else if(roles.contains("s_01_011")){
                String city=  loginUser.getSysUser().getCity();
                operationCompanyInfo.setCity(city);
            }else if(roles.contains("p_01_011")){
                String province=  loginUser.getSysUser().getProvince();
                operationCompanyInfo.setProvince(province);
            }else if(roles.contains("admin")){
                operationCompanyInfo.setProvince("370000");
            }
        }
        System.out.println("---p----"+operationCompanyInfo.getProvince()+"---c--"+operationCompanyInfo.getCity()+"----county--"+operationCompanyInfo.getCounty());
        return operationCompanyInfoMapper.groupArea(operationCompanyInfo);
    }
}
